import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CarwashDto } from './dto/carwash.dto';
import { CarwashesPaginationDto } from './dto/carwashes-pagination.dto';
import { FindDataDto } from './dto/find-data.dto';
import { City } from '../../interfaces/city.interface';

@Injectable({
  providedIn: 'root',
})
export class CarwashesService {
  constructor(private http: HttpClient) {}

  /**
   * 创建一个洗车点
   * @param carwashDto 洗车点信息
   */
  create(carwashDto: CarwashDto) {
    return this.http.post<CarwashDto>('carwashes', carwashDto);
  }

  /**
   * 通过 _id 删除洗车点
   * @param _id _id
   */
  // tslint:disable-next-line: variable-name
  deleteById(_id: string) {
    return this.http.delete(`carwashes/${_id}`);
  }

  /**
   * 通过 _id 更新洗车点信息
   * @param _id _id
   * @param carwashDto 洗车点信息
   */
  // tslint:disable-next-line: variable-name
  updateById(_id: string, carwashDto: CarwashDto) {
    return this.http.put(`carwashes/${_id}`, carwashDto);
  }

  /**
   * 查询一些洗车点
   * @param carwashesPaginationDto 分页
   */
  find(carwashesPaginationDto: CarwashesPaginationDto) {
    const { pageIndex, pageSize, keyword } = carwashesPaginationDto;
    return this.http.get<FindDataDto<CarwashDto>>('carwashes', {
      params: {
        pageIndex: pageIndex.toString(),
        pageSize: pageSize.toString(),
        keyword,
      },
    });
  }

  /**
   * 获取城市选项数据
   */
  getCitiesOptions() {
    return this.http.get<City[]>('/assets/cities.json');
  }
}
